from djcontrol import *
from Tkinter import *

import djcontrolutil

# This is a pretty sweet wrapper around tkinter widgets.
# If this doesn't have what you want, just use o.basecontrol.whatever!


class DjButton(DjControl):
	"""
	
	Properties:
	
	*text* - Text of the button. Note that the & character will be converted into an alt-key.
	*icon* - Use syntax "btn.icon = image" or "btn.icon = (image, 'center')". Use one of 'center','left','right','top','bottom','no_label'. If display is 'no_label', then only the icon is shown, creating a bitmap button.
	*onclick* - Function. Command that is called upon click.
	*enabled* - True or False. If False, the button is disabled.
	*height* - Integer, where one unit is the height of a line of text.
	*width* - Integer, where one unit is the width of a char.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'raised'. Set to 'sunken' and it will appear pressed.
	*defaultstyle* - Set to True to draw a border indicating this is the default button.
	*borderwidth* - Integer, defaults to 1 or 2.
	*font* - Set font of button text. Standard tkinter font format, such as ('Times New Roman', 12, 'normal')
	*anchor* - Set where in the button the text (or image) should be located. Use one of n, ne, e, se, s, sw, w, nw, or center. Default is center.
	*bgcolor* - Background color of the button. RGB triplet, such as (255,0,0) for pure red.
	*color* - Foreground color of the button, to use for text. RGB triplet.
	*wrapwidth* - Set when a button's text should be wrapped into multiple lines, in screen units. Default is 0 (no wrapping).
	*activebgcolor* - Background color of the button when the button is active. RGB triplet.
	*activecolor* - Foreground color of the button when the button is active. RGB triplet.
	*alignment* - Set alignment of text when there are multiple lines. One of 'left','right','center'.
	"""
	
	# Note that only names beginning with '_', or the special case basecontrol, should be used.
	_autoProperties = None
	basecontrol = 0
	
	def __init__(self, parent, strLabel='', fnCallback=None, **options):
		parent = djcontrolutil.getbasecontrol(parent)
		self.basecontrol = Button(parent)
		
		self._autoProperties = {
		# Property name,  Tkinter property name, SET wrapper, GET wrapper
		'onclick': ('command',None,None),
		'enabled': ('state', _boolToState,_stateToBool),
		'width': ('width',None,None),
		'height': ('height',None,None),
		
		'borderwidth': ('borderwidth',None,None),
		'defaultstyle': ('default',_boolTodefault,_defaultToBool),
		'borderstyle': ('relief', djcontrolutil.stringToRelief,djcontrolutil.reliefToString),
		'font': ('font',None,None),
		'wrapwidth': ('wraplength',None,None),
		'anchor': ('anchor',djcontrolutil.stringToAnchor,djcontrolutil.anchorToString),
		'alignment': ('justify',djcontrolutil.stringToJustify,djcontrolutil.justifyToString),
		'bgcolor': ('background',djcontrolutil.colorToString,djcontrolutil.stringToColor),
		'color': ('foreground',djcontrolutil.colorToString,djcontrolutil.stringToColor),
		'activebgcolor': ('activebackground',djcontrolutil.colorToString,djcontrolutil.stringToColor),
		'activecolor': ('activeforeground',djcontrolutil.colorToString,djcontrolutil.stringToColor),

		}
		
		if strLabel!='': self.text = strLabel
		if fnCallback!=None: self.onclick=fnCallback
		self.set_many(**options)
	
	def call(self):
		"""Call the callback associated with the button."""
		self.basecontrol.invoke()
	
	_customProperties = ('text','icon')
	def _lookupCustomProperties(self, bSet, strAttribute, value=None):
		if strAttribute=='text':
			if bSet:
				text, nIndex = djcontrolutil.stringToUnderlinePosition(value) # If given a string '&hello', underline the h.
				self.basecontrol.config(text=text,underline=nIndex)
				self.basecontrol.update_idletasks()
			else:
				return djcontrolutil.underlinePositionToString(self.basecontrol['text'], self.basecontrol['underline'])
		elif strAttribute=='icon':
			if bSet:
				# Either icon = image or icon = (image, 'center')
				import types
				if type(value)==types.TupleType or type(value)==types.ListType:
					image, strDisplay = value
				else:
					image, strDisplay = value, 'center' #Default to center
				self.basecontrol.config(image=image, compound=_stringToCompound(strDisplay))
				self.basecontrol.update_idletasks()
			else:
				return (self.basecontrol['image'], _compoundToString(self.basecontrol['compound']))
		else:
			raise AttributeError, strAttribute

def _boolToState(b):
	if b: return NORMAL
	else: return DISABLED
def _stateToBool(state):
	if state==DISABLED: return False
	else: return True
		
def _boolTodefault(b):
	if b: return ACTIVE
	else: return DISABLED
def _defaultToBool(state):
	if state==DISABLED: return False
	else: return True

def _stringToCompoundDict(): return {'no_label':NONE,'center':CENTER,'bottom':BOTTOM,'left':LEFT,'right':RIGHT,'top':TOP}
def _stringToCompound(value): return _stringToCompoundDict()[value] 
def _compoundToString(value): return djcontrolutil.dictInverse(_stringToCompoundDict())[value]
